<?php namespace Summer\Location\Models;

use Model;
use Winter\Storm\Database\Traits\Validation;

/**
 * Model
 */
class CityModel extends Model
{
    use Validation;

    /*
     * Disable timestamps by default.
     * Remove this line if timestamps are defined in the database table.
     */
    public $timestamps = false;


    /**
     * @var string The database table used by the model.
     */
    public $table = 'summer_location_city';

    /**
     * @var array Validation rules
     */
    public $rules = [
    ];
    /**
     * @var array Relations
     */
    public $hasMany = [
        'areas' => ['Summer\Location\Models\AreaModel','key'=>'city_id','otherKey'=>'id']
    ];

    protected static $nameList = [];

    public static function getNameList($province)
    {
        $province_id=$province->id??'';
        if (isset(self::$nameList[$province_id])) {
            return self::$nameList[$province_id];
        }
        return self::$nameList[$province_id] = self::where('province_id',$province_id)->orderBy('name', 'asc')->lists('name', 'code');
    }

    public function beforeSave()
    {
        //总共两位，前补零
        $this->city = sprintf("%02d",$this->city);
    }
}
